// component/ui/z-popover/z-popover.js
import {
  _toArray,
  _isWhatType
} from '../common/utils';

const app = getApp();

Component({

  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/
  },

  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
    },
    height: {
      type: String,
      value: '',
    },
    options: {
      type: Array,
      value: [],
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    placement: 'bottom',
    show: false
  },

  /**
   * 组件的数据监听
   */
  observers: {

  },

  /**
   * 组件的方法列表
   */
  methods: {

    placement(x, y) {
      if (y > app.globalData.windowHeight - 170) {
        this.setData({
          placement: 'top'
        })
      } else {
        this.setData({
          placement: 'bottom'
        })
      }
    },

    onClickTitle(e) {
      const touche = e.touches[0]
      this.placement(touche.clientX, touche.clientY)
      this.setData({
        show: !this.data.show
      })
      this.triggerEvent('click', this)
    },

    onClickOption(e) {
      let value = e.target.dataset.value || '';
      this.setData({
        show: false,
        placement: 'bottom',
      })
      this.triggerEvent('select', value)
    },

    onHide() {
      if (this.data.show) {
        this.setData({
          show: false,
          placement: 'bottom',
        })
      }
    },

  }
})